package com.yyk.yykywd.mvvm.ui

import android.view.View
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.google.android.material.tabs.TabLayout
import com.orhanobut.logger.Logger
import com.yyk.yykywd.*
import com.yyk.yykywd.adapter.MyPagerAdapter
import com.yyk.yykywd.base.BaseActivity
import com.yyk.yykywd.bean.BaseBean
import com.yyk.yykywd.config.HttpUrl
import com.yyk.yykywd.mvvm.event.FinishWorkDetail
import com.yyk.yykywd.mvvm.view_model.WorkDetailViewModel
import com.yyk.yykywd.util.ToAnotherActivity
import com.yyk.yykywd.util.ToastUtil
import kotlinx.android.synthetic.main.activity_work_detail.*
import kotlinx.android.synthetic.main.activity_work_detail1.*
import kotlinx.android.synthetic.main.activity_work_detail1.cl_business_process_tracking
import kotlinx.android.synthetic.main.activity_work_detail1.nv
import kotlinx.android.synthetic.main.activity_work_detail1.tv_add_return_money
import kotlinx.android.synthetic.main.activity_work_detail1.tv_agent_refund
import kotlinx.android.synthetic.main.activity_work_detail1.tv_business_follow
import kotlinx.android.synthetic.main.activity_work_detail1.tv_completion_contract
import kotlinx.android.synthetic.main.activity_work_detail1.tv_create_time
import kotlinx.android.synthetic.main.activity_work_detail1.tv_create_time_lint
import kotlinx.android.synthetic.main.activity_work_detail1.tv_founder
import kotlinx.android.synthetic.main.activity_work_detail1.tv_founder_lint
import kotlinx.android.synthetic.main.activity_work_detail1.tv_number
import kotlinx.android.synthetic.main.activity_work_detail1.tv_return_money_status
import kotlinx.android.synthetic.main.item_site.view.*
import kotlinx.android.synthetic.main.layout_load.*
import kotlinx.android.synthetic.main.rl_bottom1.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class WorkDetail1Activity : BaseActivity() {
    private lateinit var viewModel: WorkDetailViewModel

    override fun initView() {
        EventBus.getDefault().register(this)
        viewModel = ViewModelProviders.of(this).get(WorkDetailViewModel::class.java)
        with(viewModel) {
            id = intent.getStringExtra(ID)
            type = intent.getStringExtra(TYPE)
            showType = intent.getIntExtra(SHOW_TYPE, 0)
        }
        setBackArrow()
        when (viewModel.type) {
            FROM_RETURN_MONEY_AGENT, FROM_CUSTOMER_RETURN_MONEY_AGENT -> {
                setTitle("回款完成详情")
                tv_return_money_status.setBackgroundResource(R.drawable.shape_2dp3)
                tv_return_money_status.setTextColor(ContextCompat.getColor(mActivity, R.color.gray_aa))
                cl_base_info.visibility = View.GONE
                if (viewModel.showType == 1) {
                    cl_complete.visibility = View.VISIBLE
                    tv_add_proxy_site.visibility = View.VISIBLE
                    tv_agent_refund2.visibility = View.GONE
                } else {
                    cl_complete.visibility = View.GONE
                }
            }
            FROM_SIGN_CONTRACT_AGENT -> {
                setTitle("签订合同详情")
                tv_return_money_status.setBackgroundResource(R.drawable.shape_2dp5)
                tv_return_money_status.setTextColor(ContextCompat.getColor(mActivity, R.color.col_5987ff))
                if (viewModel.showType == 1) {
                    cl_base_info.visibility = View.VISIBLE
                } else {
                    cl_base_info.visibility = View.GONE
                }
            }
        }
    }

    override fun initData() {
        loading_layout.showLoading()
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_work_detail1
    }

    override fun setListener() {
        super.setListener()
        cl_business_process_tracking.setOnClickListener {
            val url = String.format(HttpUrl.BUSINESS_PROCESS_TRACKING, viewModel.id, "2")
            ToAnotherActivity.toWebActivity(mActivity, url, "业务流程跟踪")
        }

        tv_business_follow.setOnClickListener {
            ToAnotherActivity.toBusinessFollowActivity(mActivity,
                    viewModel.type,
                    viewModel.id,
                    viewModel.visitorName,
                    viewModel.visitorPosition,
                    viewModel.visitorPhone)
        }

        tv_add_return_money.setOnClickListener {
            ToAnotherActivity.toAddReturnActivity(mActivity, viewModel.position, viewModel.id, viewModel.type, viewModel.pact_id)
        }

        tv_completion_contract.setOnClickListener {
            ToAnotherActivity.toCompletionContractActivity(mActivity, viewModel.id, viewModel.pact_id,
                    viewModel.back_money_num,
                    viewModel.stillNeedMoney)
        }

        viewModel.agentDetail1.observe(this, Observer {
            it.data?.run {
                tv_number.text = operation_num
                tv_return_money_status.text = tag_client
                if (belong_type == 2) {
                    tv_founder_lint.text = "业务认领人"
                    tv_founder.text = claimname
                    tv_create_time_lint.text = "从公海认领时间"
                    tv_create_time.text = getCommonTime(update_time)
                } else {
                    tv_founder_lint.text = "业务创建人"
                    tv_founder.text = addname
                    tv_create_time_lint.text = "创建时间"
                    tv_create_time.text = getCommonTime(create_time)
                }
                if (ptype == 1) {
                    viewModel.isPerson = true
                } else {
                    viewModel.isPerson = false
                }
                tv_agency_count.text = agency_count.toString()
            }
            val list = mutableListOf("代理商基本信息")
            for ((index, s) in it.info.withIndex()) {
                list.add("代理站点" + index.plus(1))
            }
            val agentSiteAdapter = MyPagerAdapter(this, viewModel.type, list, it)

            tabLayout.removeAllTabs()
            for ((index, s) in list.withIndex()) {
                val inflate = View.inflate(mActivity, R.layout.item_site, null)
                inflate.tv.text = s
                if (s == "代理商基本信息") {
                    inflate.indicate.visibility = View.GONE
                    inflate.tv.setTextColor(ContextCompat.getColor(mActivity, R.color.col_5987ff))
                } else {
                    if (it.info[index - 1].red_dot == 1) {
                        inflate.indicate.visibility = View.VISIBLE
                    } else {
                        inflate.indicate.visibility = View.GONE
                    }
                    inflate.tv.setTextColor(ContextCompat.getColor(mActivity, R.color.black_33))
                }
                val tab = tabLayout.newTab()
                tab.customView = inflate
                tabLayout.addTab(tab)
            }

            viewPager.adapter = agentSiteAdapter

            viewPager.clearOnPageChangeListeners()
            tabLayout.clearOnTabSelectedListeners()
            // 这两个方法是手动设置 TabLayout 与 ViewPager 的关联，防止 TabLayout 自己把我们自定义的 view 销毁。
            viewPager.addOnPageChangeListener(object : TabLayout.TabLayoutOnPageChangeListener(tabLayout) {
            })
            tabLayout.addOnTabSelectedListener(object : TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
                override fun onTabSelected(tab: TabLayout.Tab) {
                    super.onTabSelected(tab)
                    val customView = tab.customView
                    customView?.tv?.setTextColor(ContextCompat.getColor(mActivity, R.color.col_5987ff))
                    when (viewModel.type) {
                        FROM_RETURN_MONEY_AGENT -> {
                            cl_base_info.visibility = View.GONE
                            cl_site.visibility = View.GONE
                            if (viewModel.showType == 1) {
                                cl_complete.visibility = View.VISIBLE
                                if (tab.position == 0) {
                                    tv_add_proxy_site.visibility = View.VISIBLE
                                    tv_agent_refund2.visibility = View.GONE
                                } else {
                                    tv_add_proxy_site.visibility = View.GONE
                                    if (it.info[tab.position - 1].is_show == 2) {
                                        cl_complete.visibility = View.GONE
                                    } else {
                                        cl_complete.visibility = View.VISIBLE
                                        tv_agent_refund2.visibility = View.VISIBLE
                                    }
                                    viewModel.pact_id = it.info[tab.position - 1].pact_id.toString()
                                    viewModel.name = it.info[tab.position - 1].agency_name ?: ""
                                    viewModel.amountCollected = it.info[tab.position - 1].already_money.toString()
                                    Logger.e(viewModel.pact_id)
                                    Logger.e(viewModel.amountCollected)
                                }
                            } else {
                                cl_complete.visibility = View.GONE
                            }
                        }
                        FROM_SIGN_CONTRACT_AGENT -> {
                            cl_complete.visibility = View.GONE
                            if (viewModel.showType == 1) {
                                if (tab.position == 0) {
                                    cl_base_info.visibility = View.VISIBLE
                                    cl_site.visibility = View.GONE
                                } else {
                                    cl_base_info.visibility = View.GONE
                                    cl_site.visibility = View.VISIBLE
                                    Logger.e(it.info[tab.position - 1].agency_name
                                            + (tab.position - 1)
                                            + it.info[tab.position - 1].tag_list.toString())
                                    when (it.info[tab.position - 1].tag_list) {
                                        2 -> {
                                            tv_add_return_money.visibility = View.VISIBLE
                                            tv_completion_contract.visibility = View.VISIBLE
                                            tv_agent_refund.visibility = View.VISIBLE
                                        }
                                        3 -> {
                                            tv_add_return_money.visibility = View.GONE
                                            tv_completion_contract.visibility = View.VISIBLE
                                            tv_agent_refund.visibility = View.VISIBLE
                                        }
                                        4 -> {
                                            tv_add_return_money.visibility = View.VISIBLE
                                            tv_completion_contract.visibility = View.GONE
                                            tv_agent_refund.visibility = View.VISIBLE
                                        }
                                        5 -> {
                                            tv_add_return_money.visibility = View.GONE
                                            tv_completion_contract.visibility = View.GONE
                                            tv_agent_refund.visibility = View.VISIBLE
                                        }
                                        6 -> {
                                            cl_site.visibility = View.GONE
                                        }
                                    }
                                    viewModel.pact_id = it.info[tab.position - 1].pact_id.toString()
                                    viewModel.amountCollected = it.info[tab.position - 1].already_money.toString()
                                    viewModel.position = tab.position - 1
                                    viewModel.name = it.info[tab.position - 1].agency_name ?: ""
                                    viewModel.stillNeedMoney = it.info[tab.position - 1].rxhk_money
                                }
                            } else {
                                cl_base_info.visibility = View.GONE
                                cl_site.visibility = View.GONE
                            }
                        }
                    }
                }

                override fun onTabUnselected(tab: TabLayout.Tab) {
                    super.onTabUnselected(tab)
                    val customView = tab.customView
                    customView?.tv?.setTextColor(ContextCompat.getColor(mActivity, R.color.black_33))
                }
            })

            loading_layout.showContent()
            nv.visibility = View.VISIBLE
        })

        viewModel.error.observe(this, Observer {

        })

        viewModel.base.observe(this, Observer<BaseBean> {
            ToastUtil.showShort(it.msg)
            if (it.code == 200) {
                finish()
            }
        })

        tv_agent_refund.setOnClickListener {
            ToAnotherActivity.toAgentRefundActivity(mActivity, viewModel.refund_num, viewModel.back_money_num,viewModel.name, viewModel.pact_id, viewModel.id, viewModel.amountCollected)
        }

        tv_add_site.setOnClickListener {
            if (viewModel.isPerson) {
                ToAnotherActivity.toUploadContractActivity(mActivity, FROM_AGENT_PERSONAL_ADD, viewModel.id)
            } else {
                ToAnotherActivity.toUploadContractActivity(mActivity, FROM_AGENT_COMPANY_ADD, viewModel.id)
            }
        }

        tv_agent_refund2.setOnClickListener {
            ToAnotherActivity.toAgentRefundActivity(mActivity, viewModel.refund_num, viewModel.back_money_num,viewModel.name, viewModel.pact_id, viewModel.id, viewModel.amountCollected)
        }

        tv_add_proxy_site.setOnClickListener {
            if (viewModel.isPerson) {
                ToAnotherActivity.toUploadContractActivity(mActivity, FROM_AGENT_PERSONAL_ADD, viewModel.id)
            } else {
                ToAnotherActivity.toUploadContractActivity(mActivity, FROM_AGENT_COMPANY_ADD, viewModel.id)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun finishDetail(event: FinishWorkDetail) {
        finish()
    }

    override fun onResume() {
        super.onResume()
        viewModel.getAgentDetail()
    }
}

